# -*- coding: UTF-8 -*-
# ！/usr/bin/env python3

import os
import logging.config


base_path = os.path.dirname(os.path.abspath(__file__))


def logger():
    logging_config = {
        "version": 1,
        "disable_existing_loggers": True,
        "formatters": {
            'colored': {
                '()': 'colorlog.ColoredFormatter',
                # 自定义颜色
                "log_colors": {
                    'DEBUG': 'cyan',
                    'INFO': 'green',
                    'WARNING': 'yellow',
                    'ERROR': 'red',
                    'CRITICAL': 'purple'
                },
                "format": "%(log_color)s[%(asctime)s] %(filename)s -> %(funcName)s line:%(lineno)d [%(levelname)s] : %(message)s"
            }
        },
        "handlers": {
            "console": {
                "class": "logging.StreamHandler",
                "level": "DEBUG",
                "formatter": "colored",
                "stream": "ext://sys.stdout"
            },
            "info_file_handler": {
                "class": "logging.handlers.RotatingFileHandler",
                "level": "INFO",
                "formatter": "colored",
                "filename": f"{base_path}/logs/info.log",
                "maxBytes": 10485760,
                "backupCount": 50,
                "encoding": "utf-8"
            },
            "error_file_handler": {
                "class": "logging.handlers.RotatingFileHandler",
                "level": "ERROR",
                "formatter": "colored",
                "filename": f"{base_path}/logs/errors.log",
                "maxBytes": 10485760,
                "backupCount": 20,
                "encoding": "utf-8"
            },
            "debug_file_handler": {
                "class": "logging.handlers.RotatingFileHandler",
                "level": "DEBUG",
                "formatter": "colored",
                "filename": f"{base_path}/logs/debug.log",
                "maxBytes": 10485760,
                "backupCount": 50,
                "encoding": "utf-8"
            },
        },
        "loggers": {
            "my_module": {"level": "ERROR", "handlers": ["console"], "propagate": False}
        },
        "root": {
            "level": "DEBUG",
            #"handlers": ["error_file_handler", "debug_file_handler", "info_file_handler"]
            "handlers": ["console", "error_file_handler", "debug_file_handler", "info_file_handler"]
        },
    }
    # 初始化日志配置
    logging.config.dictConfig(logging_config)
    return logging
